package com.lanyou.esb.cook.proxy.redis.proxy.impl;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.cook.proxy.common.Constants4Redis;
import com.lanyou.esb.cook.proxy.entity.IfAlrReqTimes;
import com.lanyou.esb.cook.proxy.redis.JedisPoolUtils;
import com.lanyou.esb.cook.proxy.redis.proxy.base.BaseStrJedisProxy;

/**
 * 接口已请求次数Redis操作代理
 * 
 * @author Davey.wu
 */
public class IfAlrReqTimesJedisProxy extends BaseStrJedisProxy<IfAlrReqTimes> {
	public IfAlrReqTimesJedisProxy() {
		this.setTimeOut(Constants4Redis.REDIS_TIMEOUT_IFACCTIMES);
	}

	@Override
	public IfAlrReqTimes get(String key) throws JedisException,
			JsonParseException, JsonMappingException, IOException {
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return null;
		}
		IfAlrReqTimes result = null;
		String value = "";

		boolean broken = false;
		try {
			value = jedis.hget(this.getKey(), key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		result = new IfAlrReqTimes();
		if (StringUtils.isNotBlank(value)) {
			return result;
		}
		try {
			result.setTimes(Integer.parseInt(value));
		} catch (Exception e) {
			result.setTimes(0);
		}
		return result;
	}

	@Override
	public String getKeyFromObject(IfAlrReqTimes value) {
		// TODO Auto-generated method stub
		return null;
	}

}
